﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Xml.Serialization;

namespace Project3.XML
{
    public class XMLSerializer
    {
        static public void SerializeToXML(MembershipConfiguration config)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(MembershipConfiguration));
            TextWriter textWriter = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + @"MembershipConfig.xml");
            serializer.Serialize(textWriter, config);
            textWriter.Close();
        }

        static public MembershipConfiguration DeserializeFromXML()
        {
            XmlSerializer deserializer = new XmlSerializer(typeof(MembershipConfiguration));
            TextReader textReader = new StreamReader(AppDomain.CurrentDomain.BaseDirectory + @"MembershipConfig.xml");
            MembershipConfiguration config;
            config = (MembershipConfiguration)deserializer.Deserialize(textReader);
            textReader.Close();

            return config;
        }
    }

    [Serializable]
    public class MembershipConfiguration
    {
        public List<string> Roles { get; set; }
        public List<SimpleUser> Users { get; set; }
        public List<UserInRole> UsersInRoles { get; set; } 
    }

    [Serializable]
    public class SimpleUser
    {
        [XmlAttribute]
        public string Username { get; set; }
        [XmlAttribute]
        public string Password { get; set; }
        [XmlAttribute]
        public string Email { get; set; }
    }

    [Serializable]
    public class UserInRole
    {
        [XmlAttribute]
        public string Username { get; set; }
        [XmlAttribute]
        public string Role { get; set; }
    }
}